<%--
  Created by IntelliJ IDEA.
  User: houxianghua
  Date: 2015/7/21
  Time: 8:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/component/common.jsp" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title></title>
    <%@include file="/WEB-INF/jsp/component/commonHead.jsp" %>
    <%@include file="/WEB-INF/jsp/component/commonBottom.jsp" %>
    <style>
        .text-center{
            text-align:center;
        }
    </style>
</head>
<%--<body onresize="reSizeParentIframe()" onload="reSizeParentIframe();">--%>
<body>
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">仪器管理</h1>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    仪器名称:<input type="text" id="instrumentName" name="instrumentName"/>
                    <button id="query" class="mk_butt" >查询</button>
                        <%--<button id="query" type="button" class="btn btn-outline btn-success">查询</button>--%>
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">
                    <div class="dataTable_wrapper">
                        <table id="example" class="table table-striped table-bordered table-hover" width="100%">
                            <thead>
                            <tr align="left">
                                <th>仪器名称</th>
                                <th>项目管理</th>
                                <th>试剂耗材管理</th>
                            </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
        </div>
        <!-- /.col-lg-12 -->
    </div>

    <script>
        var table;
        function initTable() {
            table = $("#example").DataTable({
                "dom": "<'row'<'col-xs-2'l><'#mytool.col-xs-10'><'col-xs-6'f>r>" +
                "t" +
                "<'row'<'col-xs-6'i><'col-xs-6'p>>",
                "bFilter": false,//去掉搜索框
                "bAutoWidth": true, //自适应宽度
                "sPaginationType" : "full_numbers",
//                "bDestroy" : true,
//              "asStripeClasses": [ 'strip1', 'strip2', 'strip3' ],//设置斑马条（奇偶行）的css class
                "ordering": false,//是否允许Datatables开启排序
                "bProcessing" : true,
                "bServerSide" : true,
                "sAjaxSource" : "${URL_ADM_INSTRUMENT_RELATE_SEARCH}",
                "sAjaxDataProp": "dataList",//查询后，返回的集合
                "fnServerData": function ( sSource, aoData, fnCallback ) {  //查询条件
                    aoData.push(
                            { "name": "instrumentName", "value": $("#instrumentName").val() }
                    );
                    $.ajax( {
                        "dataType": 'json',
                        "url": sSource,
                        "type":'post',
                        "data": aoData,
                        "success": fnCallback
                    } );

                },
                "aoColumns" : [ {
                    "mDataProp" : "instrumentName"
                }, {
                    "mDataProp" : "testItemNames",
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData==null){
                            sData="";
                        }
                        var updateButt = "";
                        <shiro:hasPermission name="+instrumentRelate+4">
                        updateButt += '<div style="float:right; width: 170px;">'
                                + '<input type="button" class="btn btn-outline btn-success" value="编辑" onclick="setItemModal(\''+oData.id+'\',\''+oData.testItemCodes+'\')" >'
                                + '</div>';
                        </shiro:hasPermission>
                        $(nTd).html(sData+updateButt);
                    }
                }, {
                    "mDataProp" : "names",
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData==null){
                            sData="";
                        }
                        var updateButt = "";
                        <shiro:hasPermission name="+instrumentRelate+4">
                        updateButt += '<div style="float:right; width: 170px;">'
                                + '<input type="button" class="btn btn-outline btn-success" value="编辑" onclick="setConsumablesModal(\''+oData.id+'\',\''+oData.reagentConsumableIds+'\')" >'
                                + '</div>';
                        </shiro:hasPermission>
                        $(nTd).html(sData+updateButt);
                    }
                }
                ],
                "oLanguage" : {
                    "sProcessing" : "正在加载中......",
                    "sLengthMenu" : "每页显示 _MENU_ 条记录",
                    "sZeroRecords" : "没有数据！",
                    "sEmptyTable" : "无数据存在！",
                    "sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
                    "sInfoEmpty" : "显示0到0条记录",
                    "sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
                    //"sSearch" : "搜索",
                    "oPaginate" : {
                        "sFirst" : "首页",
                        "sPrevious" : "上一页",
                        "sNext" : "下一页",
                        "sLast" : "末页"
                    }
                }
            });
        }

        //查询
        $("#query").click(function() {
            refreshTable();
        });

        //刷新table数据
        function refreshTable(){
            table.ajax.reload();
        }

        $(document).ready(function() {
            initTable();
        });


        var modalShowName = "";//当前打开的模态框名称（标记，提交模态框时使用）

        /**
         * 设置项目绑定模态框内容、样式， 并显示
         * testProjectCodes：项目code
         * */
        function setItemModal(id, ids){
            var idArrExistStr = ids.split(",");//属于该仪器的项目code数组（String类型)
            var idArrExist = [];//属于该仪器的项目code数组
            for (var i = 0; i < idArrExistStr.length; i++) {
                idArrExist.push(Number(idArrExistStr[i]));
            }
            var itemModalHtml = '<form id="itemForm" class="form-horizontal" method="post" >' +
                    '<input type="hidden" name="id" value="'+id+'" />' +
                    '<div class="form-group">';
            <c:forEach items="${testProjectList}" var="testProject">
                if($.inArray(${testProject.testItemCode},idArrExist)>=0){
                    //属于该仪器的项目
                    itemModalHtml += '<div class="col-sm-4"><label class="checkbox-inline"><input type="checkbox" checked="checked" name="ids" value="${testProject.testItemCode}">${testProject.testItemName}</label></div>';
                }else{
                    //不属于该仪器的项目
                    itemModalHtml += '<div class="col-sm-4"><label class="checkbox-inline"><input type="checkbox" name="ids" value="${testProject.testItemCode}">${testProject.testItemName}</label></div>';
                }
            </c:forEach>
            itemModalHtml += '</div></form>';
            window.parent.showModel(itemModalHtml,"项目修改");
            modalShowName = "item";
        }

        /**
         * 设置试剂耗材绑定模态框内容、样式， 并显示
         * reagentConsumableCodes：试剂耗材code
         * */
        function setConsumablesModal(id, ids){
            var idArrExistStr = ids.split(",");//属于该仪器的试剂耗材code数组（String类型)
            var idArrExist = [];//属于该仪器的试剂耗材code数组
            for (var i = 0; i < idArrExistStr.length; i++) {
                idArrExist.push(Number(idArrExistStr[i]));
            }
            var consumablesModalHtml = '<form id="consumablesForm" class="form-horizontal" method="post" >' +
                    '<input type="hidden" name="id" value="'+id+'" />' +
                    '<div class="form-group">';
            <c:forEach items="${reagentConsumableList}" var="reagentConsumable">
                if($.inArray(${reagentConsumable.id},idArrExist)>=0){
                    //属于该仪器的试剂耗材
                    consumablesModalHtml += '<div class="col-sm-4"><label class="checkbox-inline"><input type="checkbox" checked="checked" name="ids" value="${reagentConsumable.id}">${reagentConsumable.name}</label></div>';
                }else{
                    //不属于该仪器的试剂耗材
                    consumablesModalHtml += '<div class="col-sm-4"><label class="checkbox-inline"><input type="checkbox" name="ids" value="${reagentConsumable.id}">${reagentConsumable.name}</label></div>';
                }
            </c:forEach>
            consumablesModalHtml += '</div></form>';
            window.parent.showModel(consumablesModalHtml,"试剂耗材修改");
            modalShowName = "consumables";
        }

        //弹出框提交
        parent.top.$("#modal_submit_butt").click(function(){
            if(modalShowName === "item"){//修改项目提交
                $.ajax({
                    url:'${URL_ADM_INSTRUMENT_RELATE_UPDATEITEM}',
                    type:'post',
                    data:parent.top.$('#itemForm').serialize(),
                    async : false, //默认为true 异步
                    error:function(){
                        var str = 'error'
                        window.parent.showNewModelIframe('${URL_COMMON_TOTIPS}?title='+ encodeURI(encodeURI(str)),380,220)
                    },
                    success:function(data){
                        window.parent.closeModal();
                    }
                });
            }else if(modalShowName === "consumables"){//修改试剂耗材提交
                $.ajax({
                    url:'${URL_ADM_INSTRUMENT_RELATE_UPDATECONSUMABLES}',
                    type:'post',
                    data:parent.top.$('#consumablesForm').serialize(),
                    async : false, //默认为true 异步
                    error:function(){
                        alert('error');
                    },
                    success:function(data){
                        window.parent.closeModal();
                    }
                });
            }else{

            }
        });

    </script>
</body>
</html>